package com.magic233.controller;

import com.magic233.entity.Orders;
import com.magic233.entity.Users;
import com.magic233.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * ClassName: SuperAdmin
 * Description:
 * date: 2021/5/30 14:09
 * author: zouyuan
 */

@RestController
@RequestMapping("/superAdmin")
public class SuperAdminController {
    @Autowired
    private WorkTypeService workTypeService;
    @Autowired
    private WorkersService workersService;
    @Autowired
    private AdminsService adminsService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private DeptsService deptsService;

    @RequestMapping("/addWorkType")
    public Object addWorkType(HttpServletRequest request, String type) {
        System.out.println("超级管理员添加工作类型");
        if (type == null || "".equals(type)) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = workTypeService.addWorkType(user, type);
        return re;
    }

    @RequestMapping("/addDept")
    public Object addDept(HttpServletRequest request, String dept) {
        System.out.println("超级管理员添加系部");
        if (dept == null || "".equals(dept)) {
            System.out.println("参数为空");
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = deptsService.addDept(user, dept);
        return re;
    }

    @RequestMapping("/deleteWorkType")
    public Object deleteWorkType(HttpServletRequest request, String type) {
        System.out.println("超级管理员删除工作类型");
        if (type == null || "".equals(type) || "不可用".equals(type)) {
            System.out.println("参数为空");
            return 0;//参数非法
        } else {
            Users user = (Users) request.getAttribute("user");
            int re = workTypeService.deleteWorkType(user, type);
            return re;//1为删除成功
        }
    }

    @RequestMapping("/deleteDept")
    public Object deleteDept(HttpServletRequest request, String dept) {
        System.out.println("超级管理员删除系部");
        if (dept == null || "".equals(dept)) {
            System.out.println("参数为空");
            return 0;//参数非法
        } else {
            Users user = new Users();
            user.setDept(dept);
            Long count = usersService.selectCount(user);
            System.out.println("该系部:" + dept + "人员数量:" + count);
            if (count == 0) {
                Users u = (Users) request.getAttribute("user");
                int re = deptsService.deleteDept(u, dept);
                return re;//1为删除成功
            } else {
                return -1;//该系部已有人员
            }
        }
    }

    @RequestMapping("/addWorker")
    public Object addWorker(HttpServletRequest request, Long id, String type, String username, String phone) {
        System.out.println("超级管理员添加维修人员");
        if (id == null || type == null || phone == null || username == null || id == 0 || "".equals(type) || "".equals(phone) || "".equals(username)) {
            return 0;
        } else {
            Users u = usersService.selectUser(id);
            if (u == null) {
                //用户不存在，先在user表添加用户
                System.out.println("用户不存在，先在user表添加用户");
                Users user = new Users();
                user.setId(id);
                user.setPhone(phone);
                user.setUsername(username);
                user.setPassword(phone);//初始密码默认为手机号
                user.setDeleted(0);
                int re = usersService.addUser(user);
                if (re == 0) {
                    return 0;
                }
            }
            //将用户添加至workers表
            Users user = (Users) request.getAttribute("user");
            int re = workersService.addWorker(user, id, type);
            return re;
        }
    }

    @RequestMapping("/deleteWorker")
    public Object deleteWorker(HttpServletRequest request, Long id) {
        System.out.println("超级管理员删除维修人员");
        if (id == null) {
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = workersService.deleteWorker(user, id);
        return re;
    }

    @RequestMapping("/addAdmin")
    public Object addAdmin(HttpServletRequest request, Long id, String dept, String phone, String username) {
        System.out.println("超级管理员添加管理员");
        if (id == null || dept == null || phone == null || username == null || id == 0 || "".equals(dept) || "".equals(phone) || "".equals(username)) {
            return 0;
        } else {
            Users u = usersService.selectUser(id);
            if (u == null) {
                //用户不存在，先在user表添加用户
                System.out.println("用户不存在，先在user表添加用户");
                Users user = new Users();
                user.setId(id);
                user.setPhone(phone);
                user.setUsername(username);
                user.setPassword(phone);//初始密码默认为手机号
                user.setDept(dept);
                user.setDeleted(0);
                int re = usersService.addUser(user);
                if (re == 0) {
                    return 0;
                }
            }
            //将用户添加至admin表
            Users user = (Users) request.getAttribute("user");
            int re = adminsService.addAdmin(user, id);
            return re;
        }
    }

    @RequestMapping("/deleteAdmin")
    public Object deleteAdmin(HttpServletRequest request, Long id) {
        System.out.println("超级管理员删除管理员");
        if (id == null) {
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = adminsService.deleteAdmin(user, id);
        return re;
    }

    @RequestMapping("/addManager")
    public Object addManager(HttpServletRequest request, Long id, String dept, String phone, String username) {
        System.out.println("超级管理员添加部门负责人");
        if (id == null || dept == null || phone == null || username == null || id == 0 || "".equals(dept) || "".equals(phone) || "".equals(username)) {
            return 0;
        } else {
            Users u = usersService.selectUser(id);
            if (u == null) {
                //用户不存在，先在user表添加用户
                System.out.println("用户不存在，先在user表添加用户");
                Users user = new Users();
                user.setId(id);
                user.setPhone(phone);
                user.setUsername(username);
                user.setPassword(phone);//初始密码默认为手机号
                user.setDept(dept);
                user.setDeleted(0);
                int re = usersService.addUser(user);
                if (re == 0) {
                    return 0;
                }
            }
            //将用户增加Max权限---即部门负责人权限
            Users user = (Users) request.getAttribute("user");
            int re = usersService.addManager(user, id, dept);
            return re;
        }
    }

    @RequestMapping("/deleteManager")
    public Object deleteManager(HttpServletRequest request, Long id) {
        System.out.println("超级管理员删除部门负责人");
        if (id == null) {
            return 0;
        }
        Users user = (Users) request.getAttribute("user");
        int re = usersService.deleteManager(user, id);
        return re;
    }
}
